clear

use "$BS_fold/local1/decomposition_SS_ProdR_new_kl.dta"

xtset  co_code1 year, yearly
destring(nic_08_2dig), g(nic2)

gen diff_ln_IND = ln_IND - L.ln_IND
gen diff_ln_IVS = ln_IVS - L.ln_IVS
gen diff_tfpr = tfpr - L.tfpr

generate diff_r = r - L.r
foreach var in wIPT_IND_CHN wIPT_LMI_CHN_EXCL_IND rwIPT_IND_CHN rwIPT_LMI_CHN_EXCL_IND {
	generate ln_`var' = ln(`var')
	generate diff_`var' = ln_`var' - L.ln_`var'
}

foreach v in diff_tfpr diff_ln_IND diff_ln_IVS {
	drop if missing(`v')
}

** To get the final sample for all regs:
	ivreghdfe drop_term (diff_ln_IND  diff_wIPT_IND_CHN = diff_ln_IVS diff_wIPT_LMI_CHN_EXCL_IND ), a(nic2#year) first cluster(nic_08_4dig)
			generate sample0 = e(sample)
			keep if sample0 == 1

		
reghdfe diff_ln_IND diff_ln_IVS diff_wIPT_LMI_CHN_EXCL_IND , a(year) cluster(nic_08_4dig)
	eststo FSO_1

	
reghdfe diff_ln_IND  diff_ln_IVS diff_wIPT_LMI_CHN_EXCL_IND  , a(year nic2) cluster(nic_08_4dig)
	eststo FSO_2


	
reghdfe diff_ln_IND  diff_ln_IVS diff_wIPT_LMI_CHN_EXCL_IND , a(nic2#year) cluster(nic_08_4dig)
	eststo FSO_3


reghdfe diff_wIPT_IND_CHN  diff_ln_IVS diff_wIPT_LMI_CHN_EXCL_IND , a(year) cluster(nic_08_4dig)
	eststo FSI_1

	
reghdfe diff_wIPT_IND_CHN diff_ln_IVS diff_wIPT_LMI_CHN_EXCL_IND  , a(year nic2)  cluster(nic_08_4dig)
	eststo FSI_2
		
reghdfe diff_wIPT_IND_CHN diff_ln_IVS diff_wIPT_LMI_CHN_EXCL_IND , a(nic2#year) cluster(nic_08_4dig)
	eststo FSI_3

	
label var diff_ln_IND "OutputComp"
label var diff_wIPT_IND_CHN "InputAccess"
label var diff_wIPT_LMI_CHN_EXCL_IND "InputAccessIV"
label var diff_ln_IVS "OutputCompIV"
	

esttab FSO_1 FSO_2 FSO_3 FSI_1 FSI_2 FSI_3 using "$tables/FS_SVA.tex" , cells(b(nostar fmt(%9.3f)) se(par fmt(%9.3f))) nodepvars ///
eqlabels(none) mtitles("OutputComp" "OutputComp" "OutputComp" "InputAccess" "InputAccess" "InputAccess")  keep( diff_ln_IVS diff_wIPT_LMI_CHN_EXCL_IND) label replace ///
	prefoot("\hline" "Year FE & YES & YES &  NO &  YES &  YES &  NO \\" ///
	"Industry FE & NO & YES &  NO &  NO &  YES &  NO \\" /// 
	"Industry-Year FE & NO & NO &  YES &  NO &  NO &  YES \\")

clear

use "$BS_fold/local2/decomposition_SS_ProdQ_new_klme_TFPQIV.dta"
xtset  co_code1 year, yearly
destring(nic_08_2dig), g(nic2)

gen diff_ln_IND = ln_IND - L.ln_IND
gen diff_ln_IVS = ln_IVS - L.ln_IVS
gen diff_tfpr = tfpr - L.tfpr

generate diff_r = r - L.r
foreach var in wIPT_IND_CHN wIPT_LMI_CHN_EXCL_IND rwIPT_IND_CHN rwIPT_LMI_CHN_EXCL_IND {
	generate ln_`var' = ln(`var')
	generate diff_`var' = ln_`var' - L.ln_`var'
}

foreach v in diff_tfpr diff_ln_IND diff_ln_IVS {
	drop if missing(`v')
}

** To get the final sample for all regs:
	ivreghdfe drop_term (diff_ln_IND  diff_wIPT_IND_CHN = diff_ln_IVS diff_wIPT_LMI_CHN_EXCL_IND ), a(nic2#year) first cluster(nic_08_4dig)
			generate sample0 = e(sample)
			keep if sample0 == 1

		
reghdfe diff_ln_IND diff_ln_IVS diff_wIPT_LMI_CHN_EXCL_IND , a(year) cluster(nic_08_4dig)
	eststo FSO_1

	
reghdfe diff_ln_IND  diff_ln_IVS diff_wIPT_LMI_CHN_EXCL_IND  , a(year nic2) cluster(nic_08_4dig)
	eststo FSO_2


	
reghdfe diff_ln_IND  diff_ln_IVS diff_wIPT_LMI_CHN_EXCL_IND , a(nic2#year) cluster(nic_08_4dig)
	eststo FSO_3


reghdfe diff_wIPT_IND_CHN  diff_ln_IVS diff_wIPT_LMI_CHN_EXCL_IND , a(year) cluster(nic_08_4dig)
	eststo FSI_1

	
reghdfe diff_wIPT_IND_CHN diff_ln_IVS diff_wIPT_LMI_CHN_EXCL_IND  , a(year nic2)  cluster(nic_08_4dig)
	eststo FSI_2
		
reghdfe diff_wIPT_IND_CHN diff_ln_IVS diff_wIPT_LMI_CHN_EXCL_IND , a(nic2#year) cluster(nic_08_4dig)
	eststo FSI_3

	
label var diff_ln_IND "OutputComp"
label var diff_wIPT_IND_CHN "InputAccess"
label var diff_wIPT_LMI_CHN_EXCL_IND "InputAccessIV"
label var diff_ln_IVS "OutputCompIV"
	

esttab FSO_1 FSO_2 FSO_3 FSI_1 FSI_2 FSI_3 using "$tables/FS_GO.tex" , cells(b(nostar fmt(%9.3f)) se(par fmt(%9.3f))) nodepvars ///
eqlabels(none) mtitles("OutputComp" "OutputComp" "OutputComp" "InputAccess" "InputAccess" "InputAccess")  keep( diff_ln_IVS diff_wIPT_LMI_CHN_EXCL_IND) label replace ///
	prefoot("\hline" "Year FE & YES & YES &  NO &  YES &  YES &  NO \\" ///
	"Industry FE & NO & YES &  NO &  NO &  YES &  NO \\" /// 
	"Industry-Year FE & NO & NO &  YES &  NO &  NO &  YES \\")


	


